package cn.com.uthink.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

public class FileDownloadUtil {

	private static final int BUFFER_SIZE = 8192;
	private static final String CHARSET = "UTF-8";

	public static void download(String filePath, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
		download(new File(filePath), request, response);
	}

	public static void download(String fileName,byte[] data, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
		long fileSize = data.length;
		settingResponseAndRequest(request, response, fileName, fileSize);
		OutputStream os = new BufferedOutputStream(response.getOutputStream());
		os.write(data);
		IOUtils.closeQuietly(os);
	}
	
	public static void download(File file, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
		String fileName = file.getName();
		long fileSize = file.length();
		settingResponseAndRequest(request, response, file.getAbsolutePath(), file, fileName, fileSize);
		int read = 0;
		byte[] buffer = new byte[BUFFER_SIZE];
		InputStream is = new BufferedInputStream(new FileInputStream(file));
		OutputStream os = new BufferedOutputStream(response.getOutputStream());
		while ((read = is.read(buffer)) != -1) {
			os.write(buffer, 0, read);
		}
		IOUtils.closeQuietly(is);
		IOUtils.closeQuietly(os);
	}

	private static void settingResponseAndRequest(HttpServletRequest request, HttpServletResponse response, String fileName,
			long fileSize) throws UnsupportedEncodingException {
		String mimeType = request.getSession().getServletContext().getMimeType(fileName);
		if (mimeType == null || mimeType.length() == 0) {
			mimeType = "application/octet-stream;";
		}
		response.setHeader("Content-Transfer-Encoding", "binary");
		response.setContentType(mimeType + "; charset=" + CHARSET);
		String userAgent = request.getHeader("User-Agent");
		if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) {
			// IE 5.5
			response.setHeader("Content-Disposition", "filename=" + URLEncoder.encode(fileName, "UTF-8") + ";");
		} else if (userAgent != null && userAgent.indexOf("MSIE") > -1) {
			// IE 6.0
			response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8") + ";");
		} else {
			response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(CHARSET), "latin1") + ";");
		}
		if (fileSize > 0) {
			response.setHeader("Content-Length", "" + fileSize);
		}
	}
	
	private static void settingResponseAndRequest(HttpServletRequest request, HttpServletResponse response, String filePath, File file, String fileName,
			long fileSize) throws UnsupportedEncodingException {
		String mimeType = request.getSession().getServletContext().getMimeType(file.getName());
		if (mimeType == null || mimeType.length() == 0) {
			mimeType = "application/octet-stream;";
		}
		response.setHeader("Content-Transfer-Encoding", "binary");
		response.setContentType(mimeType + "; charset=" + CHARSET);
		String userAgent = request.getHeader("User-Agent");
		if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) {
			// IE 5.5
			response.setHeader("Content-Disposition", "filename=" + URLEncoder.encode(filePath, "UTF-8") + ";");
		} else if (userAgent != null && userAgent.indexOf("MSIE") > -1) {
			// IE 6.0
			response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8") + ";");
		} else {
			response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(CHARSET), "latin1") + ";");
		}
		if (fileSize > 0) {
			response.setHeader("Content-Length", "" + fileSize);
		}
	}
}